﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MandalaySoft.Common;
using System.Data.SqlClient;
using System.Data;

namespace SIMS.BL
{
    public class CoursePeriodDB
    {
        MyDatabase _db = new MyDatabase();

        public DataTable GetCoursePeriodList(int CourseID, out MyError error)
        {
            DataTable _dt = new DataTable("tblCourse");
            SortedList _paramList = new SortedList();
            _paramList.Add("@CourseID", CourseID);

            _dt = _db.GetDataTable("sprocCoursePeriodSelectList", CommandType.StoredProcedure, _paramList, out error);
            return _dt;
        }

        public int Delete(int CoursePeriodID, out MyError error)
        {
            SortedList _paramList = new SortedList();
            _paramList.Add("@CoursePeriodID", CoursePeriodID);

            return _db.ExecuteNonSql("sprocCoursePeriodDeleteSingleItem", CommandType.StoredProcedure, _paramList, out error);
        }

        public CoursePeriod GetItem(int CoursePeriodID)
        {
            CoursePeriod item = new CoursePeriod();

            SortedList _paraList = new SortedList();
            _paraList.Add("@CoursePeriodID", CoursePeriodID);

            MyError _error = new MyError();
            SqlDataReader _reader = _db.GetSqlDataReader("sprocCoursePeriodSelectSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
            item.Error = _error;

            if (_error.ErrorNo == 0 && _reader.Read())
            {
                item = FillData(_reader, item);
            }

            return item;
        }

        public CoursePeriod Save(CoursePeriod item)
        {
            try
            {
                SortedList _paraList = new SortedList();
                _paraList.Add("@CoursePeriodID", item.CoursePeriodID);
                _paraList.Add("@CourseID", item.CourseID);
                _paraList.Add("@PeriodName", item.PeriodName);
                _paraList.Add("@ShortName", item.ShortName);
                _paraList.Add("@TeacherID", item.TeacherID);
                _paraList.Add("@AssTeacherID", item.AssTeacherID);
                _paraList.Add("@RoomID", item.RoomID);
                _paraList.Add("@PeriodID", item.PeriodID);
                _paraList.Add("@SeatNo", item.SeatNo);
                _paraList.Add("@GenderRestriction", item.GenderRestriction);
                _paraList.Add("@GradeScaleID", item.GradeScaleID);
                _paraList.Add("@DayList", item.DayList);
                _paraList.Add("@CalendarID", item.CalendarID);
                _paraList.Add("@Credit", item.Credit);
                _paraList.Add("@IsTakeAttendence", item.IsTakeAttendence);
                _paraList.Add("@IsAffectHonorRoll", item.IsAffectHonorRoll);
                _paraList.Add("@IsHalfDay", item.IsHalfDay);
                _paraList.Add("@IsCourseWeighted", item.IsCourseWeighted);
                _paraList.Add("@IsAllowTeacherGradeScale", item.IsAllowTeacherGradeScale);
                _paraList.Add("@IsAffectClassRank", item.IsAffectClassRank);
                _paraList.Add("@ParentCoursePeriodID", item.ParentCoursePeriodID);

                _paraList.Add("@IsActive", item.IsActive);
                _paraList.Add("@CreatedBy", item.CreatedBy);
                _paraList.Add("@ModifiedBy", item.ModifiedBy);

                MyError _error = new MyError();
                Int64 _retValue = _db.Execute("sprocCoursePeriodInsertUpdateSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
                item.Error = _error;

                if (_retValue > 0)
                {
                    item.CoursePeriodID = Convert.ToInt32(_retValue);
                    item.Error.ErrorMessage = "Saved successfully!";
                }
                else
                {
                    if (_error.ErrorNo == 0)
                    {
                        item.Error.ErrorNo = 10002;
                        item.Error.ErrorLocation = "CoursePeriod";
                        item.Error.ErrorMessage = "No row affected.";
                    }
                }

            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10001;
                item.Error.ErrorLocation = "CoursePeriod";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CoursePeriod");
            }

            return item;
        }

        private CoursePeriod FillData(IDataReader aRow, CoursePeriod item)
        {
            try
            {
                item = new CoursePeriod();
                item.CoursePeriodID = aRow.GetInt32(aRow.GetOrdinal("CoursePeriodID"));
                item.CourseID = aRow.GetInt32(aRow.GetOrdinal("CourseID"));
                item.PeriodName = aRow.GetString(aRow.GetOrdinal("PeriodName"));
                if (aRow["ShortName"] != DBNull.Value)
                {
                    item.ShortName = aRow.GetString(aRow.GetOrdinal("ShortName"));
                }
                item.TeacherID = aRow.GetInt32(aRow.GetOrdinal("CoursePeriodID"));
                if (aRow["AssTeacherID"] != DBNull.Value)
                {
                    item.AssTeacherID = aRow.GetInt32(aRow.GetOrdinal("AssTeacherID"));
                }
                if (aRow["RoomID"] != DBNull.Value)
                {
                    item.RoomID = aRow.GetInt32(aRow.GetOrdinal("RoomID"));
                }
                if (aRow["PeriodID"] != DBNull.Value)
                {
                    item.PeriodID = aRow.GetInt32(aRow.GetOrdinal("PeriodID"));
                }
                if (aRow["SeatNo"] != DBNull.Value)
                {
                    item.SeatNo = aRow.GetInt16(aRow.GetOrdinal("SeatNo"));
                }
                if (aRow["GenderRestriction"] != DBNull.Value)
                {
                    item.GenderRestriction = aRow.GetByte(aRow.GetOrdinal("GenderRestriction"));
                }
                if (aRow["GradeScaleID"] != DBNull.Value)
                {
                    item.GradeScaleID = aRow.GetInt32(aRow.GetOrdinal("GradeScaleID"));
                }
                if (aRow["DayList"] != DBNull.Value)
                {
                    item.DayList = aRow.GetString(aRow.GetOrdinal("DayList"));
                }
                if (aRow["CalendarID"] != DBNull.Value)
                {
                    item.CalendarID = aRow.GetInt32(aRow.GetOrdinal("CalendarID"));
                }
                if (aRow["Credit"] != DBNull.Value)
                {
                    item.Credit = aRow.GetDecimal(aRow.GetOrdinal("Credit"));
                }
                if (aRow["IsTakeAttendence"] != DBNull.Value)
                {
                    item.IsTakeAttendence = aRow.GetBoolean(aRow.GetOrdinal("IsTakeAttendence"));
                }
                if (aRow["IsAffectHonorRoll"] != DBNull.Value)
                {
                    item.IsAffectHonorRoll = aRow.GetBoolean(aRow.GetOrdinal("IsAffectHonorRoll"));
                }
                if (aRow["IsHalfDay"] != DBNull.Value)
                {
                    item.IsHalfDay = aRow.GetBoolean(aRow.GetOrdinal("IsHalfDay"));
                }
                if (aRow["IsCourseWeighted"] != DBNull.Value)
                {
                    item.IsCourseWeighted = aRow.GetBoolean(aRow.GetOrdinal("IsCourseWeighted"));
                }
                if (aRow["IsAllowTeacherGradeScale"] != DBNull.Value)
                {
                    item.IsAllowTeacherGradeScale = aRow.GetBoolean(aRow.GetOrdinal("IsAllowTeacherGradeScale"));
                }
                if (aRow["IsAffectClassRank"] != DBNull.Value)
                {
                    item.IsAffectClassRank = aRow.GetBoolean(aRow.GetOrdinal("IsAffectClassRank"));
                }
                if (aRow["ParentCoursePeriodID"] != DBNull.Value)
                {
                    item.ParentCoursePeriodID = aRow.GetInt32(aRow.GetOrdinal("ParentCoursePeriodID"));
                }
                item.IsActive = aRow.GetBoolean(aRow.GetOrdinal("IsActive"));
                item.CreatedBy = aRow.GetInt32(aRow.GetOrdinal("CreatedBy"));
                if (_db.ColumnExists(aRow, "CreatedByName") && aRow["CreatedByName"] != DBNull.Value)
                {
                    item.CreatedByName = aRow.GetString(aRow.GetOrdinal("CreatedByName"));
                }
                item.CreatedDate = aRow.GetDateTime(aRow.GetOrdinal("CreatedDate"));
                item.ModifiedBy = aRow.GetInt32(aRow.GetOrdinal("ModifiedBy"));
                if (_db.ColumnExists(aRow, "ModifiedByName") && aRow["ModifiedByName"] != DBNull.Value)
                {
                    item.ModifiedByName = aRow.GetString(aRow.GetOrdinal("ModifiedByName"));
                }
                item.ModifiedDate = aRow.GetDateTime(aRow.GetOrdinal("ModifiedDate"));
            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10003;
                item.Error.ErrorLocation = "CoursePeriod";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CoursePeriod");
            }

            return item;
        }
    }
}
